package com.fone.player.view;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.fone.player.ApplicationManage;
import com.fone.player.R;
import com.fone.player.client.VgdetailRst;
import com.fone.player.client.VgdetailRst.From;
import com.fone.player.util.ScreenUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 视频来源PopupWindow
 * 
 * @author kongxiaojun
 * @since 2014-5-20
 */
public class VideoFromPopupWindow {

	private List<VgdetailRst.From> forms;
	private PopupWindow popupWindow;
	private ListView listView;
	private ItemClickCallBack callBack;
	private View contentView;
	private From currentFrom;
	
	DisplayImageOptions options;

	public VideoFromPopupWindow(Context mContext, From currentFrom, List<From> forms, ItemClickCallBack callBack) {
		super();
		this.forms = forms;
		this.currentFrom = currentFrom;
		this.callBack = callBack;
		popupWindow = new PopupWindow(mContext);
		contentView = LayoutInflater.from(mContext).inflate(R.layout.video_from_popupwindow, null);
		listView = (ListView) contentView.findViewById(R.id.video_from_list);
		listView.setAdapter(new FromAdapter());
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				From from = VideoFromPopupWindow.this.forms.get(position);
				VideoFromPopupWindow.this.callBack.onItemclick(from,position);
			}
		});

		popupWindow.setContentView(contentView);
		popupWindow.setWidth(ScreenUtil.dp2px(106));
		popupWindow.setHeight(forms.size() * ScreenUtil.dp2px(46) + 20);
		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.source_knowun)
		.showImageForEmptyUri(R.drawable.source_knowun)
		.showImageOnFail(R.drawable.source_knowun)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.build();
	}

	/**
	 * 弹出popwindow
	 * 
	 * @param view
	 *            以这个布局的右下角为基准
	 * @param shareInfo
	 *            分享信息
	 */
	public void show(View view) {
		if (popupWindow != null) {
			popupWindow.showAtLocation(view, Gravity.RIGHT | Gravity.TOP, 2, view.getHeight() + ScreenUtil.getStatusBarHeight());
		}
	}

	/**
	 * 隐藏popwindow
	 */
	public void dismiss() {
		if (popupWindow != null && popupWindow.isShowing()) {
			popupWindow.dismiss();
		}
	}

	public interface ItemClickCallBack {
		void onItemclick(VgdetailRst.From from,int position);
	}

	private class FromAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			if (forms == null) {
				return 0;
			}
			return forms.size();
		}

		@Override
		public Object getItem(int position) {
			if (forms == null || forms.size() == 0) {
				return null;
			} else {
				return forms.get(position);
			}
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			VgdetailRst.From from = forms.get(position);
			convertView = LayoutInflater.from(ApplicationManage.getAplicationManageInstance()).inflate(R.layout.video_from_item, null);
			ImageView logo = (ImageView) convertView.findViewById(R.id.from_logo);
			if (TextUtils.isEmpty(from.logo)) {
				logo.setImageResource(R.drawable.source_knowun);
			} else {
				ImageLoader.getInstance().displayImage(from.logo, logo, options);
			}
			if (currentFrom.fromid == from.fromid) {
				// 背景色
				convertView.setBackgroundResource(R.color.video_series_item_watched);
			} else {
				convertView.setBackgroundResource(R.drawable.video_from_list_bg_selector);
			}
			return convertView;
		}

	}
	
	public boolean isShowing() {
		return popupWindow.isShowing();
	}

	public void setCurrentFrom(From from) {
		this.currentFrom = from;
	}
}
